
package br.ucb.musicbay.musica;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

public class MusicaDao implements MusicaInterfaceDao{

	private transient Session session;
	
	public void setSession(Session session){
		this.session = session;
	}
	
	@Override
	public boolean save(Musica musica) {
		try{
			this.session.save(musica);
			return true;
		}catch(Exception e){
			throw new HibernateException("ERRO! ERRO - save(Musica musica) "+new Date());
		}
	}

	@Override
	public boolean delete(Musica[] musica) {
		try{
			for (Musica musica2 : musica) {
				session.delete(musica2);
			}
			return true;
		}catch (Exception e) {
			throw new HibernateException("ERRO! ERRO - delete(Musica[] musica) "+new Date());
		}	

	}

	@Override
	public boolean update(Musica musica) {
		try {
			this.session.merge(musica);
			return true;
		} catch (Exception e) {	
			e.printStackTrace();		
		}	
		return false;
	}

	@Override
	public Musica select(Musica musica) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Musica> recoverAll() {
		Criteria criteria = this.session.createCriteria(Musica.class).addOrder(Order.asc("nome"));
		return criteria.list();
	}

	@Override
	public Musica recoverById(Integer id) {
		return (Musica)session.get(Musica.class, id);
	}

}
